package com.magicsoft.share.ui.main;

import android.content.Intent;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.main.SMSCode;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.customview.vcedittext.VerificationAction;
import com.magicsoft.share.customview.vcedittext.VerificationCodeEditText;
import com.magicsoft.share.presenter.main.SMSVerificationPresenter;
import com.magicsoft.share.utils.AlerterUtils;
import com.magicsoft.share.utils.Format;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.main.ISMSVerificationView;

import butterknife.BindView;
import butterknife.OnClick;

public class SMSVerificationActivity extends BaseActivity<ISMSVerificationView, SMSVerificationPresenter> implements ISMSVerificationView {

    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.edt_code)
    VerificationCodeEditText edtCode;
    private String mobileStr;
    private CountDownTimer timer;
    private String TAG = "SMSVerificationActivity";
    private int inputPasswordType;
    private String codeStr;
    private String method = "1000";

    @Override
    public SMSVerificationPresenter initPresenter() {
        return new SMSVerificationPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_smsverification;
    }

    @Override
    public void initData() {
        Log.d(TAG, "initData: ");
        mobileStr = getIntent().getStringExtra("mobile");
        inputPasswordType = getIntent().getIntExtra("inputPasswordType", -1);
        if (inputPasswordType == Constant.INPUT_PASSWORD_FIRST) {
            method = "1000";
        } else {
            method = "1001";
        }


    }

    @Override
    public void initView() {

        tvTitle.setText("短信验证");
        tvMobile.setText(Format.hideMobilePhone4(mobileStr));
        edtCode.setOnVerificationCodeChangedListener(new VerificationAction.OnVerificationCodeChangedListener() {
            @Override
            public void onVerCodeChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void onInputCompleted(CharSequence s) {
                codeStr = edtCode.getText().toString();
                mPresenter.checkSMSCode(mobileStr, codeStr, method);
            }
        });
    }

    @Override
    public void requestData() {
        Log.d(TAG, "requestData: ");
        mPresenter.sendCode(method, mobileStr);

    }


    @OnClick({R.id.ibtn_left, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.btn_next:
                mPresenter.sendCode(method, mobileStr);
                initTimer();

                break;
        }
    }

    @Override
    public void sendCodeSuccess(SMSCode smsCode) {
        ToastUtils.showToast(this, "验证码发送成功！");
//        Log.d(TAG, "sendCodeSuccess: " + smsCode.toString() + smsCode.getVcode());
        initTimer();

    }

    @Override
    public void checkSMSCodeSuccess() {
        Log.d(TAG, "checkSMSCodeSuccess: ");
        Intent intent = new Intent(SMSVerificationActivity.this, InputPasswordActivity.class);
        intent.putExtra("inputPasswordType", inputPasswordType);
        intent.putExtra("mobileStr", mobileStr);
        intent.putExtra("codeStr", codeStr);
        startActivity(intent);
        finish();
    }

    @Override
    public void checkSMSCodeFail(String errorMsg) {
        AlerterUtils.warning(SMSVerificationActivity.this, errorMsg);
    }


    /**
     * 60s倒计时
     */
    private void initTimer() {

        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                btnNext.setEnabled(false);
                btnNext.setText("重新发送(" + (millisUntilFinished / 1000) + ")s");
                btnNext.setBackgroundResource(R.drawable.bg_shape_gray);
            }

            @Override
            public void onFinish() {
                btnNext.setEnabled(true);
                btnNext.setText("重新发送");
                btnNext.setBackgroundResource(R.drawable.bg_shape_black);
            }
        };
        timer.start();
    }
}
